
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");

function Utils () {
    this.nativeJSON = Components.classes["@mozilla.org/dom/json;1"].createInstance(Components.interfaces.nsIJSON); 
    this.wrappedJSObject = this;
}
Utils.prototype = {
    classDescription: "Utils component",
    classID:          Components.ID("{818f1d52-6cfb-4a7d-a6de-683fd077d67f}"),
    contractID:       "@berjon.com/utils;1",
    QueryInterface:   XPCOMUtils.generateQI(),

    // files
    file:    function (path) {
        var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
        if (path) file.initWithPath(path);
        return file;
    },
    
    append:     function (dir, path) {
        var ret = dir.clone();
        path.split("/").forEach(function (step) { ret.append(step) });
        return ret;
    },
    
    fileURI:    function (file) {
        return Components.classes["@mozilla.org/network/protocol;1?name=file"]
                         .getService(Components.interfaces.nsIFileProtocolHandler)
                         .getURLSpecFromFile(file);
    },
    
    
    // debug
    warnHandlers:   [],
    addWarnHandler:    function (func) {
        this.warnHandlers.push(func);
    },
    warn:    function (str) {
        for (var i = 0; i < this.warnHandlers.length; i++) {
            this.warnHandlers[i](str);
        }
    },

    errorHandlers:   [],
    addErrorHandler:    function (func) {
        this.errorHandlers.push(func);
    },
    error:    function (str) {
        for (var i = 0; i < this.errorHandlers.length; i++) {
            this.errorHandlers[i](str);
        }
    },
    
    widListHandlers:   [],
    addWidListHandler:    function (func) {
        this.widListHandlers.push(func);
    },
    widListChange:    function () {
        for (var i = 0; i < this.widListHandlers.length; i++) {
            this.widListHandlers[i](str);
        }
    },
    
    dmp:    function (obj) {
        var str = "";
        for (var k in obj) { str += k + ": " + obj[k] + "\n"; }
        return str;
    },

};
var components = [Utils];
function NSGetModule (compMgr, fileSpec) {
    return XPCOMUtils.generateModule(components);
}
